<template>
  <a-drawer
    title="奖学金管理修改"
    width=450
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="referenceManageEditVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form=form>
      <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="院系">
              <a-select :allowClear="true" showSearch optionFilterProp="children"  v-decorator="[
                  'ssyxm',
                  {
                    rules: [{ required: true, message: '请输入院系!' }],
                  },
                  {initialValue:postParams.ssyxm}
                ]" >
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="年级">
              <a-select :allowClear="true" showSearch optionFilterProp="children"  v-decorator="[
                  'zsnd',
                  {
                    rules: [{ required: true, message: '请输入年级!' }],
                  },
                  {initialValue:postParams.zsnd}
                ]" >
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="培养层次">
              <a-select :allowClear="true" showSearch optionFilterProp="children"  v-decorator="[
                  'pyccm',
                  {
                    rules: [{ required: true, message: '请输入培养层次!' }],
                  },
                  {initialValue:postParams.pyccm}
                ]" >
                <a-select-option v-for="i in TYPE0310" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="奖学金类型">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="[
                  'jxjdjdm',
                  {
                    rules: [{ required: true, message: '请输入奖学金类型!' }],
                  },
                  {initialValue:postParams.jxjdjdm}
                ]" @change="jbChange">
                <a-select-option v-for="i in TYPE_jxjlx" :key="i.id" :value="i.code" >{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
           <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="奖学金级别">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="[
                  'jxjlxm',
                  {
                    rules: [{ required: true, message: '请输入奖学金级别!' }],
                  },
                  {initialValue:postParams.jxjdjdm}
                ]" >
                <a-select-option v-for="i in selectMenu.lx" :key="i.id" :value="i.code" >{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="奖学金金额">
              <a-input placeholder="请输入..." v-decorator="[
                  'jxje',
                  {
                    rules: [{ required: true, message: '请输入奖学金金额!' }],
                  },
                  {initialValue:postParams.jxje}
                ]"/>
            </a-form-item>
          </a-col>
      </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'
const selectArr = ['TYPE0126','TYPE_jxjlx','TYPE0311','TYPE0312','TYPE0310']
const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}
export default {
  name: 'setupAdd',
  props: {
    referenceManageEditVisiable: {
      default: false
    },
    info: {
      default: () => []
    }
  },
  components: {
    STable
  },
  data () {
    return {
      formItemLayout,
      loading: false,
      postParams: {
       
      },
      selectMenu: {
        yx: [], // 院系
        zy: [],
        lx: []
      },
      doctoralAdmissionYear:''
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
       TYPE0126: state => state.app['TYPE0126'],
      TYPE_jxjlx:state => state.app['TYPE_jxjlx'],
      TYPE0311:state => state.app['TYPE0311'],
      TYPE0312:state => state.app['TYPE0312'],
      TYPE0310:state => state.app['TYPE0310']
    })
  },
  beforeCreate() {
      this.form = this.$form.createForm(this)
    },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  watch:{
    info(val){
        this.$nextTick(()=>{
        this.form.setFieldsValue({'jxjdjdm':val.jxjdjdm})
        this.form.setFieldsValue({'jxje':val.jxje})
        this.form.setFieldsValue({'jxjlxm':val.jxjlxm})
        this.form.setFieldsValue({'pyccm':val.pyccm})
        this.form.setFieldsValue({'ssyxm':val.ssyxm})
        this.form.setFieldsValue({'zsnd':val.zsnd})
        })
        if(val.jxjdjdm == '1'){
       this.$set(this.selectMenu,'lx',this.TYPE0311)
        }else {
      this.$set(this.selectMenu,'lx',this.TYPE0312)
        }
    }
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })
       this.$api.base.getSchoolYear().then(res=>{
        this.doctoralAdmissionYear=res.data.doctoralAdmissionYear;
      })
    },
    reset () {
      this.data = []
    },
    // 选择院系
    jbChange(code){
    this.$set(this.selectMenu,'lx',[])
    this.form.setFieldsValue({jxjlxm:''})
        if(code){
    if(code == '1'){
       this.$set(this.selectMenu,'lx',this.TYPE0311)
     }else {
      this.$set(this.selectMenu,'lx',this.TYPE0312)
    }
        }
    },
    // 关闭
    onClose () {
      this.reset()
      this.form.resetFields();
        this.$set(this.selectMenu,'zy','')
      this.$set(this.selectMenu,'fx','')
      this.$set(this.selectMenu,'ds','')
      this.$emit('close')
    },
    // 保存
    handleSubmit () {
       this.form.validateFields((err, values) => {
          if (err) {
            this.$message.error('请输入必填项')
            return
          }
          var params = Object.assign({id:this.info.id},this.form.getFieldsValue())
          this.$api.student.setupUpdate(params).then(res => {
          
            this.$emit("success");
             this.form.resetFields()
            
          })
        })
      
    },
    handleDelete (key) {
      const dataSource = [...this.data]
      this.data = dataSource.filter(item => item.key !== key)
    }
  }
}
</script>
